#--
# Author:: Jarod Reid
# Copyright (c) 2008 Jarod Reid
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ++
  
module Q2Engine
  # = EngineConfiguration
  # 
  # The EngineConfiguration class stores the q2engine configuration variables.
  # Variables are accessed by method and are stored by defaul tin Q2CONFIG.
  # For example,
  #     QCONFIG.world_name => 'q2engine'
  # Or
  #     QCONFIG.copyright => 'Copyright (c) q2engine'
  class EngineConfiguration
      
    # You can initialize an EngineConfiguration instance with a filename for an
    # alternate configuration file. All configuration files are loaded from the
    # config directory.
    def initialize(file='q2config.yml') #:nodoc:
      @config = YAML::load(File.new(File.join(RAILS_ROOT, 
        'config', file), 'r'))
      @config_file = file
    end
    
    # Updates configuration instance with the provided hash. This method uses
    # the dump_hash method to write to file, so it creates a backup file in
    # case a rollback is needed.
    def update(hash)
      hash.each do |k,v|
        @config[k] = v
      end
      self.dump_hash(@config)
      self.reload!
    end
    
    # Dumps given hash to file and creates a backup of the old configuration.
    # The method accepts an alternate file name to dump to, otherwise it dumps
    # to the same file used to create the instance. This method will not write
    # to the q2config.yml file in test mode.
    def dump_hash(hash, file=@config_file)
      return false if(@config_file == 'q2config.yml' &&
        RAILS_ENV == 'test')
      File.open(File.join(RAILS_ROOT, 'config', 
        file + '.bak.yml'), 'w') do |out|
        YAML::dump(@config, out)
      end
      File.open(File.join(RAILS_ROOT, 'config', file), 'w') do |out|
        YAML::dump(hash, out)
      end
      return true
    end
    
    # Reloads configuration instance from file.
    def reload!
      @config = YAML::load(File.new(File.join(RAILS_ROOT, 
        'config', @config_file), 'r'))
      return true
    end

    
    
    private
    
    # This handles displaying configuration variables as needed.
    def method_missing(method_id)
      path = method_id.to_s.split('_')
      key = path[0].to_sym
      if(@config[key].class == String || @config[key].class == Array)
        return @config[key]
      end
      if(@config[key].class == Hash)
        value = path[1].to_sym
        raise NoMethodError if @config[key][value].nil?
        return @config[key][value]
      end
      raise NoMethodError
    end
    
    
  end
  
  
  
end